package com.lianniu.mgr.orderManage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.orderManage.vo.OrderDetailVo;
import com.lianniu.mgr.util.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.dict.service.IDictionaryService;
import com.lianniu.mgr.orderManage.dao.IOrderManageDao;
import com.lianniu.mgr.orderManage.service.IOrderManageService;
import com.lianniu.mgr.orderManage.util.OrderManageUtil;
import com.lianniu.mgr.orderManage.vo.OrderConditionVo;
import com.lianniu.mgr.orderManage.vo.OrderInfoVo;
import com.lianniu.mgr.orderManage.vo.ShipmentVo;
import com.lianniu.mgr.pagination.PaginationInfo;

@Service
public class OrderManageServiceImpl implements IOrderManageService {

  private static final Logger logger = LoggerFactory.getLogger(OrderManageServiceImpl.class);
  @Autowired
  IOrderManageDao orderManageDao;

  @Autowired
  IDictionaryService dictionaryService;

  @Override
  @Transactional
  public BaseResult getOrderListForPage(OrderConditionVo condition, PaginationInfo paginationInfo) {
    condition.setPaginationInfo(paginationInfo);
    List<OrderInfoVo> orderList = orderManageDao.getOrderListForPage(condition);
    Map<String, Object> retmap = new HashMap<String, Object>();
    retmap.put("total", paginationInfo.getTotalRecord());
    retmap.put("curPage", paginationInfo.getCurPage());
    retmap.put("rows", orderList);

    return BaseResult.success(retmap);
  }

  public BaseResult getOrderById(String orderId) {
    logger.info("根据ID获取订单" + orderId);
    OrderInfoVo orderInfo = orderManageDao.getOrderById(orderId);
    orderInfo.setOrderTypeName((String) dictionaryService
        .getDictNameByDictVal(OrderManageUtil.MAIN_ORDER_TYPE_DICTYPE, orderInfo.getOrderType()).getData());
    orderInfo.setPayTypeName((String) dictionaryService
        .getDictNameByDictVal(OrderManageUtil.PAY_TYPE_DICTYPE, orderInfo.getOrderType()).getData());
    orderInfo.setPayWayName((String) dictionaryService
        .getDictNameByDictVal(OrderManageUtil.PAY_WAY_DICTYPE, orderInfo.getOrderType()).getData());
    logger.info(String.format("获取到的订单信息[%s]", JSON.toJSONString(orderInfo)));

    return BaseResult.success(orderInfo);
  }

  @Override
  public BaseResult shipment(ShipmentVo ship) {
      orderManageDao.shipment(ship);
    return BaseResult.success();
  }

  @Override
  public BaseResult modifyOrder(OrderInfoVo orderInfovo,OrderDetailVo orderDetailVo) {
    logger.info("modifyOrder begin");
    logger.info("modifyOrder orderId:" +orderInfovo.getId().toString());
    logger.info("modifyOrder: input data check begin");

    BaseResult baseResult;
    if (StringUtil.isEmpty(orderInfovo.getId().toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    int order=orderManageDao.updateOrderInfo(orderInfovo);
    int orderdetail=orderManageDao.updateOrderDetail(orderDetailVo);

    logger.info("modifyOrder: input data check end");

    return BaseResult.success(order+orderdetail);
  }

  @Override
  public BaseResult modifyStaff(String orderId, String staff) {
    logger.info("modifyStaff begin");
    logger.info("modifyStaff orderId:" +orderId.toString());
    logger.info("modifyStaff: input data check begin");

    BaseResult baseResult;
    if (StringUtil.isEmpty(orderId.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(staff.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("modifyStaff: input data check end");

    return BaseResult.success(orderManageDao.updateOrderStaff(orderId,staff));
  }

  @Override
  public BaseResult modifyComment(String orderId, String commResult, String comment) {
    logger.info("modifyComment begin");
    logger.info("modifyComment orderId:" +orderId.toString());
    logger.info("modifyComment: input data check begin");

    BaseResult baseResult;
    if (StringUtil.isEmpty(orderId.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(commResult.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("modifyComment: input data check end");
    orderManageDao.updateOrderStatus(orderId,"07");
    int i=0;
    if(orderManageDao.getOrdercommentByOrderId(orderId)>0)
    {
      i=orderManageDao.updateOrderComment(orderId,commResult,comment);
    }
    else
    {
      i=orderManageDao.insertOrdercomment(orderId,commResult,comment);
    }

    return BaseResult.success(i);
  }

  @Override
  public BaseResult updateOrderStatus(String orderID, String status) {
    logger.info("updateOrderStatus begin");
    logger.info("updateOrderStatus orderID:" +orderID.toString());
    logger.info("updateOrderStatus: input data check begin");

    BaseResult baseResult;
    if (StringUtil.isEmpty(orderID.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(status.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }

    logger.info("updateOrderStatus: input data check end");

    return BaseResult.success(orderManageDao.updateOrderStatus(orderID,status));
  }


  @Override
  public BaseResult updateOrderStatus(String orderID, String status,String cancelReason) {
    logger.info("updateOrderStatus begin");
    logger.info("updateOrderStatus orderID:" +orderID.toString());
    logger.info("updateOrderStatus: input data check begin");

    BaseResult baseResult;
    if (StringUtil.isEmpty(orderID.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(status.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(cancelReason.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("updateOrderStatus: input data check end");

    return BaseResult.success(orderManageDao.cancelOrderStatus(orderID,status,cancelReason));
  }

  @Override
  public BaseResult updateOrderSF(String orderId,String status,String searchFinishedRemark, String searchFinishedImg1, String searchFinishedImg2) {
    logger.info("updateOrderSF begin");
    logger.info("updateOrderSF orderID:" +orderId.toString());
    logger.info("updateOrderSF: input data check begin");

    BaseResult baseResult;
    if (StringUtil.isEmpty(orderId.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(searchFinishedRemark.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(status.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }

    logger.info("updateOrderSF: input data check end");

    return BaseResult.success(orderManageDao.updateOrderSF(orderId,status,searchFinishedRemark,searchFinishedImg1,searchFinishedImg2));
  }

  @Override
  public BaseResult updateOrderFA(String orderId, String status, String paidRemark, String paidImgUrl1, String paidImgUrl2) {
    logger.info("updateOrderFA begin");
    logger.info("updateOrderFA orderID:" +orderId.toString());
    logger.info("updateOrderFA: input data check begin");

    BaseResult baseResult;
    if (StringUtil.isEmpty(orderId.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(paidRemark.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(status.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }

    logger.info("updateOrderFA: input data check end");

    return BaseResult.success(orderManageDao.updateOrderFA(orderId,status,paidRemark,paidImgUrl1,paidImgUrl2));
  }

  @Override
  public BaseResult updateOrderDL(String orderId, String status, String deliveRemark, String deliveImgUrl1, String deliveImgUrl2) {
    logger.info("updateOrderDL begin");
    logger.info("updateOrderDL orderID:" +orderId.toString());
    logger.info("updateOrderDL: input data check begin");

    BaseResult baseResult;
    if (StringUtil.isEmpty(orderId.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(deliveRemark.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(status.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }

    logger.info("updateOrderDL: input data check end");

    return BaseResult.success(orderManageDao.updateOrderDL(orderId,status,deliveRemark,deliveImgUrl1,deliveImgUrl2));
  }

  @Override
  public BaseResult updateOrderCF(String orderId, String status, String confirmRemark, String confirmImg1, String confirmImg2) {
    logger.info("updateOrderCF begin");
    logger.info("updateOrderCF orderID:" +orderId.toString());
    logger.info("updateOrderCF: input data check begin");

    BaseResult baseResult;
    if (StringUtil.isEmpty(orderId.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(confirmRemark.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    if (StringUtil.isEmpty(status.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }

    logger.info("updateOrderCF: input data check end");

    return BaseResult.success(orderManageDao.updateOrderCF(orderId,status,confirmRemark,confirmImg1,confirmImg2));
  }

  @Override
  public BaseResult updateOrderDelFlag(String orderID) {
    // TODO Auto-generated method stub
    logger.info("updateOrderDelFlag begin");
    logger.info("updateOrderDelFlag orderID:" +orderID.toString());
    logger.info("updateOrderDelFlag: input data check begin");

    BaseResult baseResult;
    if (StringUtil.isEmpty(orderID.toString())) {
      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      return baseResult;
    }
    logger.info("updateOrderDelFlag: input data check end");

    return BaseResult.success(orderManageDao.updateOrderDelFlag(orderID));
  }
}
